﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATest.Bll.Repositories
{
    public class QuestionRepository : ATest.Bll.Repositories.IQuestionRepository
    {
        private ATestDataContext _DBContext;
        private IQueryable<Question> _Data;

        public QuestionRepository()
        {
            _DBContext = new ATestDataContext();
            _Data = _DBContext.Questions.Where(x => !x.Deleted && !x.Test.Deleted);
        }

        public QuestionRepository(IQueryable<Question> data)
        {
            _Data = data.Where(x => !x.Deleted && !x.Test.Deleted);
        }

        public Question GetById(int id)
        {
            return _Data.SingleOrDefault(x => x.Id == id);
        }

        public IQueryable<Question> GetAllByTestId(int testId)
        {
            return _Data.Where(x => x.TestId == testId);
        }

        public void Insert(Question question)
        {
            if (_DBContext != null)
            {
                _DBContext.Questions.InsertOnSubmit(question);
            }
        }

        public void Delete(Question question)
        {
            question.Deleted = true;
        }

        public void SaveChanges()
        {
            if (_DBContext != null)
            {
                _DBContext.SubmitChanges();
            }
        }

    }
}
